package com.jisheng.xb.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.jisheng.xb.dao.UserMapper;
import com.jisheng.xb.entity.User;

@Controller
@RequestMapping("user")
public class UserController {
	
	@Autowired
	UserMapper userMapper;
	
	@RequestMapping(value = {"list"}, method = { RequestMethod.POST,RequestMethod.GET})
	public ModelAndView index(Model model,
			HttpServletRequest request,
			HttpServletResponse response){
		ModelAndView mv = new ModelAndView("write");
		return mv;
	}
	
	@RequestMapping(value={"save"},method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String  save(User user,
			HttpServletRequest request,
			HttpServletResponse response){
		user.setLevel(1);
		user.setType(1);
		String msg = "保存成功";
		userMapper.insert(user);
		return JSONObject.toJSONString(msg);
		                              
	} 
	
	@RequestMapping(value={"update"},method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String  update(User user,
			HttpServletRequest request,
			HttpServletResponse response){
		String msg = "更新成功";
		if(user.getId()==null){
			msg = "更新失败";
			return JSONObject.toJSONString(msg);
		}
		userMapper.updateByPrimaryKeySelective(user);
		return JSONObject.toJSONString(msg);
		                              
	} 
	
	@RequestMapping(value={"delete"},method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String  delete(@RequestParam Integer id,
			HttpServletRequest request,
			HttpServletResponse response){
		String msg = "删除成功";
		userMapper.deleteByPrimaryKey(id);
		return JSONObject.toJSONString(msg);
		                              
	}
}
